package org.linuxsogood.boot.config;

import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author honway.liu
 * @date 2018/8/5 下午10:21
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
public class ResponseBodyWraperHandler implements HandlerMethodReturnValueHandler {

    private final HandlerMethodReturnValueHandler delegate;

    public ResponseBodyWraperHandler(HandlerMethodReturnValueHandler delegate) {
        this.delegate = delegate;
    }

    @Override
    public boolean supportsReturnType(MethodParameter returnType) {
        return true;
    }

    @Override
    public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
        ApiResponse response = new ApiResponse();
        response.setData(returnValue);
        delegate.handleReturnValue(response, returnType, mavContainer, webRequest);
    }
}
